import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../provider/LoginProvider.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late TextEditingController _controllerName = TextEditingController();

  late TextEditingController _controllerCity = TextEditingController();

  void getShare() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    var name = await prefs.get("name");
    var phone = await prefs.get("phone");
    var city = await prefs.get("city");

    context.read<LoginProvider>().setNames(name.toString());
    context.read<LoginProvider>().setPhones(phone.toString());
    context.read<LoginProvider>().setCityName(city.toString());
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getShare();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controllerName.dispose();
    _controllerCity.dispose();
    super.dispose();
  }


  final ImagePicker _picker = ImagePicker();

// 获取图片方法
  Future getImage() async {
    final XFile? image =
    await _picker.pickImage(source: ImageSource.camera, maxWidth: 400);

    uploadImg(image!.path);

  }


// 上传图片的方法
  void uploadImg(imageUrl) async{
    FormData formData = FormData.fromMap({
      "file": await MultipartFile.fromFile(
          imageUrl,
          filename: "avatar.img"
      )
    });
    var result = await new Dio().post("https://188l877k45.zicp.vip/uploadExcelPictures", data: formData);
    print(result);
    context.read<LoginProvider>().setAliertAddress(result.toString().length > 10 ? result.toString().substring(0,11)+"...":result.toString());
    Navigator.of(context).pushNamed("csm");

  }

  void sys()async{

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      alignment: Alignment.center,
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          InkWell(
            onTap: () {
              Navigator.of(context).pushNamed("ditie");
            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      color: Colors.blue,
                      image: DecorationImage(
                          image: AssetImage("lib/assets/ykm/shenzditie.png"),
                          fit: BoxFit.fill)),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () {
              Navigator.of(context).pushNamed("ykm");
            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      color: Colors.blue,
                      image: DecorationImage(
                          image: AssetImage("lib/assets/ykm/yuekangmasss.png"),
                          fit: BoxFit.fill)),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () {
              Navigator.of(context).pushNamed("system");
            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Color.fromRGBO(0, 154, 68, 1),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.settings,
                        size: 40,
                        color: Colors.white,
                      ),
                      Text(
                        " 修改信息",
                        style: TextStyle(fontSize: 26, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          // InkWell(
          //   onTap: () {
          //     Navigator.of(context).pushNamed("hsm");
          //   },
          //   child: Container(
          //     height: 60,
          //     width: MediaQuery.of(context).size.width,
          //     child: Padding(
          //       padding: EdgeInsets.only(left: 90, right: 90),
          //       child: Container(
          //         width: 80,
          //         height: 60,
          //         decoration: BoxDecoration(
          //           borderRadius: BorderRadius.circular(20),
          //           color: Color.fromRGBO(0, 154, 68, 1),
          //         ),
          //         child: Row(
          //           mainAxisAlignment: MainAxisAlignment.center,
          //           children: [
          //             Icon(
          //               Icons.star,
          //               size: 40,
          //               color: Colors.white,
          //             ),
          //             Text(
          //               " 我 的 码",
          //               style: TextStyle(fontSize: 26, color: Colors.white),
          //             )
          //           ],
          //         ),
          //       ),
          //     ),
          //   ),
          // ),
          SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return CupertinoAlertDialog(
                      title: Text("请输入您的姓名和要去的场所"),
                      content: Column(
                        children: <Widget>[
                          SizedBox(
                            height: 10,
                          ),
                          // TextInputField(
                          //   controller: _controllerName,
                          //   labelText: "username",
                          //   icon: Icons.account_circle,
                          //
                          // ),
                          // TextInputField(
                          //   controller: _controllerCity,
                          //   labelText: "address",
                          //   icon: Icons.fast_forward,
                          // ),
                          TextField(
                            controller: _controllerName,
                            decoration: InputDecoration(
                              labelText: "姓名"
                            ),
                          ),
                          TextField(
                            controller: _controllerCity,
                            decoration: InputDecoration(
                                labelText: "场所"
                            ),
                          ),
                        ],
                      ),
                      actions: <Widget>[
                        CupertinoDialogAction(
                          child: Text("取消"),
                          onPressed: () {
                            Navigator.pop(context);
                            print("取消");
                          },
                        ),
                        CupertinoDialogAction(
                          child: Text("确定"),
                          onPressed: () {
                            //Navigator.pop(context);
                            print("确定");
                            if (_controllerName.text.toString() != "null") {
                              context.read<LoginProvider>().setAlertName(
                                  _controllerName.text.toString());
                            }

                            if (_controllerCity.text.toString() != "null") {
                              context.read<LoginProvider>().setAliertAddress(
                                  _controllerCity.text.toString());
                            }

                            //Navigator.pop(context);

                            if (_controllerName.text.toString() != "null" && _controllerCity.text.toString() != "null") {
                              // Future.delayed(Duration(milliseconds: 1000),(){
                              //
                              // });
                              Navigator.of(context).pushNamed("csm");
                            }

                          },
                        ),
                      ],
                    );
                  });
              // Navigator.of(context).pushNamed("csm");
            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Color.fromRGBO(0, 154, 68, 1),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.fast_forward,
                        size: 40,
                        color: Colors.white,
                      ),
                      Text(
                        " 场所码(输入)",
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          // InkWell(
          //   onTap: () {
          //
          //     //Navigator.of(context).pushNamed("sys");
          //     //拍照图像识别场所码
          //     getImage();
          //
          //   },
          //   child: Container(
          //     height: 60,
          //     width: MediaQuery.of(context).size.width,
          //     child: Padding(
          //       padding: EdgeInsets.only(left: 90, right: 90),
          //       child: Container(
          //         width: 80,
          //         height: 60,
          //         decoration: BoxDecoration(
          //           borderRadius: BorderRadius.circular(20),
          //           color: Color.fromRGBO(0, 154, 68, 1),
          //         ),
          //         child: Row(
          //           mainAxisAlignment: MainAxisAlignment.center,
          //           children: [
          //             Icon(
          //               Icons.fast_forward,
          //               size: 40,
          //               color: Colors.white,
          //             ),
          //             Text(
          //               " 场所码(识别) ",
          //               style: TextStyle(fontSize: 20, color: Colors.white),
          //             )
          //           ],
          //         ),
          //       ),
          //     ),
          //   ),
          // ),
          InkWell(
            onTap: () {

              //Navigator.of(context).pushNamed("sys");
              //拍照图像识别场所码
              //getImage();
              Navigator.of(context).pushNamed("blue");

            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Color.fromRGBO(0, 154, 68, 1),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.fast_forward,
                        size: 40,
                        color: Colors.white,
                      ),
                      Text(
                        " 蓝牙通讯blue ",
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () {

              //Navigator.of(context).pushNamed("sys");
              //拍照图像识别场所码
              //getImage();
              Navigator.of(context).pushNamed("speed");

            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Color.fromRGBO(0, 154, 68, 1),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.fast_forward,
                        size: 40,
                        color: Colors.white,
                      ),
                      Text(
                        " 遥控快艇 ",
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () {

              //Navigator.of(context).pushNamed("sys");
              //拍照图像识别场所码
              //getImage();
              Navigator.of(context).pushNamed("pumppage");

            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Color.fromRGBO(0, 154, 68, 1),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.wifi_protected_setup,
                        size: 40,
                        color: Colors.white,
                      ),
                      Text(
                        " 浇花系统 ",
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () {

              //Navigator.of(context).pushNamed("sys");
              //拍照图像识别场所码
              //getImage();
              Navigator.of(context).pushNamed("ysy");

            },
            child: Container(
              height: 60,
              width: MediaQuery.of(context).size.width,
              child: Padding(
                padding: EdgeInsets.only(left: 90, right: 90),
                child: Container(
                  width: 80,
                  height: 60,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Color.fromRGBO(0, 154, 68, 1),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.fast_forward,
                        size: 40,
                        color: Colors.white,
                      ),
                      Text(
                        " 萤石云 ",
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 2,
          ),
          // InkWell(
          //   onTap: () {
          //
          //     //Navigator.of(context).pushNamed("sys");
          //     //拍照图像识别场所码
          //     //getImage();
          //     Navigator.of(context).pushNamed("video");
          //
          //   },
          //   child: Container(
          //     height: 60,
          //     width: MediaQuery.of(context).size.width,
          //     child: Padding(
          //       padding: EdgeInsets.only(left: 90, right: 90),
          //       child: Container(
          //         width: 80,
          //         height: 60,
          //         decoration: BoxDecoration(
          //           borderRadius: BorderRadius.circular(20),
          //           color: Color.fromRGBO(0, 154, 68, 1),
          //         ),
          //         child: Row(
          //           mainAxisAlignment: MainAxisAlignment.center,
          //           children: [
          //             Icon(
          //               Icons.fast_forward,
          //               size: 40,
          //               color: Colors.white,
          //             ),
          //             Text(
          //               " video ",
          //               style: TextStyle(fontSize: 20, color: Colors.white),
          //             )
          //           ],
          //         ),
          //       ),
          //     ),
          //   ),
          // ),
          // Padding(
          //   padding: EdgeInsets.only(left: 20),
          //   child: Container(
          //     height: 80,
          //     width: MediaQuery.of(context).size.width,
          //     alignment: Alignment.bottomLeft,
          //     child: Text("粤康码: "+
          //       "" +
          //           context.read<LoginProvider>().getNames().toString() +
          //           "  " +
          //           context.read<LoginProvider>().getPhones().toString() +
          //           "  " +
          //           context.read<LoginProvider>().getCityName().toString(),
          //       style: TextStyle(
          //           fontSize: 16,
          //           fontWeight: FontWeight.w600,
          //           color: Colors.white),
          //     ),
          //   ),
          // ),
          // Padding(
          //   padding: EdgeInsets.only(left: 20),
          //   child: Container(
          //     height: 40,
          //     width: MediaQuery.of(context).size.width,
          //     alignment: Alignment.bottomLeft,
          //     child: Text("场所码: "+
          //       "" +
          //           context.read<LoginProvider>().getAliertName().toString() +
          //           "  " +
          //           context.read<LoginProvider>().getAliertAddress().toString(),
          //       style: TextStyle(
          //           fontSize: 16,
          //           fontWeight: FontWeight.w600,
          //           color: Colors.white),
          //     ),
          //   ),
          // ),

        ],
      ),
    );
  }
}
